package com.maomao.Thread;

public class SynchronizedTest {

    private boolean ready = false;

    private int result = 0;
    private int number = 1;

    public synchronized void write(){
        ready = true;
        number = 2;
    }

    public synchronized void read(){
        if (ready) {
            result = number * 3;
        }
        System.out.println("result is " + result);
    }

    private class TestThread extends Thread {
        private boolean flag;
        public TestThread(boolean flag) {
            this.flag = flag;
        }

        @Override
        public void run() {
            if (flag) {
                write();
            } else {
                read();
            }
        }
    }

    public static void main(String[] args) {
        SynchronizedTest test = new SynchronizedTest();
        test.new TestThread(true).start();
        test.new TestThread(false).start();


    }

}
